/**
 * Copyright &copy; 2018-2018 <a href="#">聚成科技</a> All rights reserved.
 */
package com.jckj.core.controller;

import com.jckj.core.annotation.LogTitle;
import com.jckj.core.entity.Office;
import com.jckj.core.service.OfficeService;
import com.jckj.core.util.ClassUtils;
import com.jckj.core.util.StringUtils;
import com.jckj.core.validator.groups.Add;
import com.jckj.core.vos.LoginUserVo;
import com.jckj.core.vos.OfficeVo;
import com.jckj.core.vos.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;

/**
 * <p>
 *     科室管理控制器
 * </p>
 *
 * @author 刘志远
 * @date 18-1-30
 */
@RequestMapping("/admin")
@Controller
public class DepartController {


    private final static Logger LOGGER = LoggerFactory.getLogger(DepartController.class);

    @Autowired
    OfficeService officeService;

    @Autowired
    LoginUserVo loginUserVo;

    @RequestMapping("/deptIndex")
    public String deptIndex(){
        return "deptIndex";
    }

    @RequestMapping("/listAllOffice")
    @ResponseBody
    public Result getAll(){
        return Result.ok(officeService.listAll());
    }

    /**
     * 删除科室
     * @param officeId 科室ID
     * @return
     */
    @RequestMapping(value="/delOffice", method= RequestMethod.POST)
    @ResponseBody
    @LogTitle(title="删除科室")
    public Result delDept(String officeId){
        if(StringUtils.isEmpty(officeId)){
            return Result.invalidParams("请选择要删除科室！");
        }
        officeService.delete(officeId);
        return Result.ok();
    }

    /**
     *
     * @return
     */
    @RequestMapping(value="/addOffice",method= RequestMethod.POST)
    @ResponseBody
    @LogTitle(title="新增科室")
    public Result addOffice(@ModelAttribute @Validated(value = Add.class) OfficeVo officeVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Office office = new Office();
        try {
            ClassUtils.copyValue(officeVo, office);
        } catch (Exception e) {
            LOGGER.error("拷贝数据时出现错误:%s", e.fillInStackTrace());
            e.printStackTrace();
        }
        office.setId(StringUtils.genId());
        office.setCreateTime(Calendar.getInstance().getTime());
        office.setCreateBy(loginUserVo.getId());
        if(StringUtils.isEmpty(office.getPid())){
            office.setPid(null);
        }
        officeService.add(office);
        return Result.ok();
    }

    /**
     * 检测科室编号是否重复
     * @param officeCode
     * @return
     */
    @RequestMapping(value="/checkOfficeCode",method= RequestMethod.GET)
    @ResponseBody
    public Boolean checkOfficeCode(String officeCode){
        long count = officeService.checkOfficeCode(officeCode);
        if(count>0){
            return false;
        }
        return true;
    }
}
